package com.hundsun.cprs.yyt.gateway.service.uc.impl;

import com.hundsun.cprs.yyt.common.enums.EnumBusinessCode;
import com.hundsun.cprs.yyt.common.exception.ServiceCodeException;
import com.hundsun.cprs.yyt.gateway.enums.EnumFundBankNo;
import com.hundsun.cprs.yyt.gateway.response.BaseResponse;
import com.hundsun.cprs.yyt.gateway.service.uc.BankBindingService;
import com.hundsun.cprs.yyt.gateway.service.uc.ClearingChannelFactory;
import com.hundsun.cprs.yyt.gateway.service.uc.ClearingChannelService;
import com.hundsun.cprs.yyt.service.common.dto.FundCloseInfo;
import com.hundsun.cprs.yyt.service.common.dto.FundOpenInfo;
import com.hundsun.cprs.yyt.service.common.service.RemoteTradeFundService;
import org.apache.commons.lang.Validate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * 清算中心银行绑卡实现类
 * @author herong22384
 * @date 20190301
 *
 * */
@Service
public class ClearingBankBindingServiceImpl implements BankBindingService {

    @Autowired
    private RemoteTradeFundService remoteTradeFundService;
    @Autowired
    private ClearingChannelFactory clearingChannelFactory;

    @Override
    public String getBankNo() {
        return EnumFundBankNo.CLEARING.getCode();
    }

    @Override
    public void bindingValidate(FundOpenInfo request, BaseResponse response) {
        Validate.notEmpty(request.getChannelBankNo(),"银行子渠道必填");
        ClearingChannelService clearingChannelService = clearingChannelFactory.getBankBindingService(request.getChannelBankNo());
        if(clearingChannelService == null){
            throw new ServiceCodeException(EnumBusinessCode.BUSINESS_1000.getErrorNo(),"清算中心不支持[" + request.getChannelBankNo() + "]的子渠道");
        }
        clearingChannelService.bindingValidate(request,response);
        //直接绑卡
        binding(request, response);
    }

    @Override
    public void binding(FundOpenInfo request, BaseResponse response) {
        remoteTradeFundService.open(request);
    }

    @Override
    public void unBinding(FundCloseInfo request, BaseResponse response) {
        remoteTradeFundService.close(request);
    }
}
